package com.hdyg.sixg.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.AreaAdapter;
import com.hdyg.sixg.app.MvpApplication;
import com.hdyg.sixg.mvp.Contrant.CRegistForgetpwd;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PRegistForgetPwd;
import com.hdyg.sixg.util.CountDownTimerUtil;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.SPUtils;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author CZB
 * @describe 注册/忘记密码界面
 * @time 2019/4/3 10:08
 */
public class RegistForgetPwdActivity extends BaseActivity implements CRegistForgetpwd.IVRegistForgetpwd {

    public final static int REGIST_CODE = 0x000001;     //注册
    public final static int FORGET_PWD_CODE = 0x000002; //忘记密码

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.et_invite_code)
    EditText etInviteCode;
    @BindView(R.id.ll_invite_code)
    LinearLayout llInviteCode;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.tv_login_pwd)
    TextView tvLoginPwd;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.ll_login_pwd)
    LinearLayout llLoginPwd;
    @BindView(R.id.et_sure_login_pwd)
    EditText etSureLoginPwd;
    @BindView(R.id.ll_sure_login_pwd)
    LinearLayout llSureLoginPwd;
    @BindView(R.id.et_trans_pwd)
    EditText etTransPwd;
    @BindView(R.id.ll_trans_pwd)
    LinearLayout llTransPwd;
    @BindView(R.id.et_sure_trans_pwd)
    EditText etSureTransPwd;
    @BindView(R.id.ll_sure_trans_pwd)
    LinearLayout llSureTransPwd;
    @BindView(R.id.bt_regist)
    Button btRegist;

    private int INTENT_CODE;
    private String refer,       //推荐人
            username,           //用户名
            quhao,              //区号
            phone,              //手机号
            code,               //验证码
            login_password,     //登录密码
            confirm_login,      //确认登录密码
            payment_password,   //交易密码
            confirm_pay;        //确认交易密码
    private CRegistForgetpwd.IPRegistForgetpwd mPresenter;
    private PopupWindow mPopupWindow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_regist_forget_pwd;
    }

    @Override
    protected void initView() {
        INTENT_CODE = getIntent().getIntExtra(SpMsg.INTENT_CODE, REGIST_CODE);//默认注册
        if (INTENT_CODE == REGIST_CODE) {
            tvTopTitle.setText(R.string.regist_title);
            btRegist.setText(R.string.regist_title);
        } else {
            tvTopTitle.setText(R.string.forget_pwd_title);
            btRegist.setText(R.string.forget_pwd_title);
            llInviteCode.setVisibility(View.GONE);
            llName.setVisibility(View.GONE);
            llTransPwd.setVisibility(View.GONE);
            llSureTransPwd.setVisibility(View.GONE);
            tvLoginPwd.setText(R.string.regist_pwd_2);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void createPresenter() {
        mPresenter = new PRegistForgetPwd(this);
    }

    @OnClick({R.id.ll_top_left, R.id.tv_code, R.id.tv_area, R.id.bt_regist})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_top_left:
                finish();
                break;
            case R.id.tv_area:  //区号
                showAreaPop(v);
                break;
            case R.id.tv_code:
                String codeType = "";
                phone = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    toastNotifyShort(R.string.regist_phone_hint);
                    return;
                }
                if (TextUtils.isEmpty(quhao)) {
                    toastNotifyShort(R.string.regist_area_select);
                    return;
                }
                if (INTENT_CODE == REGIST_CODE) {
                    codeType = BaseUrl.CODE_TYPE_REGIST;
                } else {
                    codeType = BaseUrl.CODE_TYPE_FORGETPWD;
                }
                mPresenter.pGetSendCode(BaseUrl.SEND_CODE_URL, GetParamUtil.getSendCodeParam(quhao, phone, codeType));
                break;
            case R.id.bt_regist:
                refer = etInviteCode.getText().toString().trim();
                username = etName.getText().toString().trim();
                phone = etPhone.getText().toString().trim();
                code = etCode.getText().toString().trim();
                login_password = etLoginPwd.getText().toString().trim();
                confirm_login = etSureLoginPwd.getText().toString().trim();
                payment_password = etTransPwd.getText().toString().trim();
                confirm_pay = etSureTransPwd.getText().toString().trim();

                if (INTENT_CODE == REGIST_CODE) {
                    if (TextUtils.isEmpty(refer)) {
                        toastNotifyShort(R.string.regist_invite_code_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(username)) {
                        toastNotifyShort(R.string.regist_name_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(quhao)) {
                        toastNotifyShort(R.string.regist_area_select);
                        return;
                    }
                    if (TextUtils.isEmpty(phone)) {
                        toastNotifyShort(R.string.regist_phone_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(code)) {
                        toastNotifyShort(R.string.regist_code_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(login_password)) {
                        toastNotifyShort(R.string.regist_pwd_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(confirm_login) || !login_password.equals(confirm_login)) {
                        toastNotifyShort(R.string.toast_login_pwd);
                        return;
                    }
                    if (TextUtils.isEmpty(payment_password)) {
                        toastNotifyShort(R.string.regist_trans_pwd_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(confirm_pay) || !payment_password.equals(confirm_pay)) {
                        toastNotifyShort(R.string.toast_trans_pwd);
                        return;
                    }
                    mPresenter.pGetRegist(BaseUrl.REGIST_URL,
                            GetParamUtil.getRegistParam(refer, username, quhao, phone, code, login_password, confirm_login, payment_password, confirm_pay));
                } else {
                    if (TextUtils.isEmpty(phone)) {
                        toastNotifyShort(R.string.regist_phone_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(quhao)) {
                        toastNotifyShort(R.string.regist_area_select);
                        return;
                    }
                    if (TextUtils.isEmpty(code)) {
                        toastNotifyShort(R.string.regist_code_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(login_password)) {
                        toastNotifyShort(R.string.regist_pwd_hint);
                        return;
                    }
                    if (TextUtils.isEmpty(confirm_login) || !login_password.equals(confirm_login)) {
                        toastNotifyShort(R.string.toast_login_pwd);
                        return;
                    }
                    mPresenter.pGetForgetPwd(BaseUrl.FORGET_PWD_URL, GetParamUtil.getForgetPwdaram(phone, code, login_password, confirm_login));
                }
                break;
        }
    }

    private void showAreaPop(View v) {
        if (MvpApplication.countryBeanList == null) {
            toastNotifyShort(R.string.toast_area);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_area, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_area);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        AreaAdapter areaAdapter = new AreaAdapter(R.layout.item_area, MvpApplication.countryBeanList);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            tvArea.setText(MvpApplication.countryBeanList.get(position).getCountry());
            tvArea.setTextColor(getResources().getColor(R.color.white));
            quhao = MvpApplication.countryBeanList.get(position).getCode();
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, v, 0);

    }

    private void registSuccessMethod(String toast) {
        toastNotifyShort(toast);
        SPUtils.put(SpMsg.USERNAME,phone);
        SPUtils.put(SpMsg.USERPASSWORD,login_password);
        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CountDownTimerUtil.getInstance().stopTimer();
    }

    @Override
    public void vGetRegistSuccess(String dataBean) {
        registSuccessMethod(dataBean);
    }

    @Override
    public void vGetSendCodeSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        CountDownTimerUtil.getInstance().startTimer(tvCode, 60);
    }

    @Override
    public void vGetForgetPwdSuccess(String dataBean) {
        registSuccessMethod(dataBean);
    }
}
